package com.ruoyi.inducePest.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inducePest.domain.ChangeRecord;
import com.ruoyi.inducePest.domain.device.InduceDeviceInfo;
import com.ruoyi.inducePest.mapper.ChangeRecordMapper;
import com.ruoyi.inducePest.mapper.device.InduceDeviceMapper;
import com.ruoyi.inducePest.service.ChangeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: ruoyi_202130504106_李航
 * @description:
 * @author: LiHang
 * @create: 2022-05-16 18:35
 **/
@Service
public class ChangeRecordServiceImpl implements ChangeRecordService {

    @Autowired
    private ChangeRecordMapper changeRecordMapper;

    @Autowired
    private InduceDeviceMapper induceDeviceMapper;

    @Override
    public TableDataInfo selectInfoByCondition(ChangeRecord changeRecord, Integer pageNum, Integer pageSize) {
        if(changeRecord.getChangeInfoId()==null && changeRecord.getCameraId()!=null) {
            InduceDeviceInfo induceDeviceInfo = new InduceDeviceInfo();
            induceDeviceInfo.setCameraId(changeRecord.getCameraId());
            List<InduceDeviceInfo> induceDeviceInfos = induceDeviceMapper.selectDeviceInfoByCondition(induceDeviceInfo);
            changeRecord.setChangeInfoId(induceDeviceInfos.get(0).getId());
        }
        if(changeRecord.getChangeInfoId()!=null){
            changeRecord.setCameraId(null);
        }
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<ChangeRecord> infoByCondition = changeRecordMapper.getInfoByCondition(changeRecord);
        PageInfo<ChangeRecord> pageInfo = new PageInfo<>(infoByCondition);
        return getTableDataInfo(infoByCondition,pageInfo);
    }

    private TableDataInfo getTableDataInfo(List<ChangeRecord> changeRecords, PageInfo<ChangeRecord> pageInfo) {

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(changeRecords);
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }
}
